package com.example.demo.LeetCode;

import com.example.demo.LeetCode.list.ListNode;

import java.util.HashMap;
import java.util.Map;

public class CopyRandomList {
    public ListNode copyRandomList(ListNode head) {
        Map<ListNode,ListNode> locations = new HashMap<>();
        ListNode p = head;

        while (p!= null){
          // 复制节点值
          locations.put(p,new ListNode(p.val));
          p = p.next;
        }
        p = head;
        while (p!= null){
            locations.get(p).next = locations.get(p.next);
            locations.get(p).random = locations.get(p.random);
            p = p.next;
        }
        return locations.get(head);
    }


}
